package cn.sumpu.app.coolsk.biz.impl;



//import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.coolsk.biz.TemplateBiz;
import cn.sumpu.app.coolsk.dao.TemplateDao;
import cn.sumpu.app.coolsk.domain.Template;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class TemplateBizImpl implements TemplateBiz{

	private TemplateDao templateDao;
	
	public void setTemplateDao(TemplateDao templateDao) {
		this.templateDao = templateDao;
	}

	@Override
	public PageModel getTemplateList(Integer page, Integer pagesize,String searchName) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		if(!Common.empty(searchName)){
			map.put("searchName", "%"+searchName+"%");
		}
		int count = templateDao.getTemplateCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(templateDao.getTemplate(map));
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}

	@Override
	public Template getTemplateById(String name) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<Template> tlist = templateDao.getTemplate(map);
		if(tlist != null && tlist.size() > 0){
			return tlist.get(0);
		}
		return null;
	}

	@Override
	public Template saveTemplate(Template template) throws AppBusinessException {
		templateDao.updateTemplate(template);
		return template;
	}
	
	@Override
	public List<Template> getTemplateList(String searchName) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		if(!Common.empty(searchName)){
			map.put("searchName", "%"+searchName+"%");
		}
		List<Template> templateList= templateDao.getTemplate(map);
		return templateList;
	}

}
